Application à la lecture d’une image using System.Net; // pour WebClient using System.Windows.Media.Imaging; // pour BitmapImage ..... WebClient client = new WebClient(); client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted); client.OpenReadAsync(new Uri("VG2.jpg", UriKind.Relative)); ..... void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { try { BitmapImage image = new BitmapImage(); image.SetSource(e.Result); img.Source = image; } catch (Exception exc) { ..... // erreur lors de l’accès, avec message d’erreur dans exc.Message } } Imports System.Net ' pour WebClient Imports System.Windows.Media.Imaging ' pour BitmapImage ..... Dim client = New WebClient() client.OpenReadAsync(New Uri("VG2.jpg", UriKind.Relative), 0) AddHandler client.OpenReadCompleted, AddressOf client_OpenReadCompleted ..... Private Sub client_OpenReadCompleted(ByVal sender As Object, _ ByVal e As OpenReadCompletedEventArgs) Try Dim image As New BitmapImage() image.SetSource(e.Result) img.Source = image Catch exc As Exception ..... ' message d’erreur dans exc.Message End Try End Sub Application à la lecture d’un fichier XML Brel 1929 Brassens 1921 Ferré 1916 using System.Net; ..... WebClient client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); client.DownloadStringAsync(new Uri("Artistes.xml", UriKind.Relative)); ..... void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { try { string s = e.Result; ..... .. contenu du fichier XML dans s .. } catch (Exception exc) { ..... } } using System.Xml.Linq; ..... XElement xml = XElement.Parse(s); var listeArtistes = from p in xml.Elements("Artiste") select p; foreach (var p in listeArtistes) { .. nom dans p.Element("Nom").Value .. .. année de naissance dans p.Element("AN").Value .. } Imports System.Xml.Linq ..... Dim client = New WebClient() client.DownloadStringAsync(New Uri("Artistes.xml", UriKind.Relative), 0) AddHandler client.DownloadStringCompleted, _ AddressOf client_DownloadStringCompleted ..... Private Sub client_DownloadStringCompleted(ByVal sender As Object, _ ByVal e As DownloadStringCompletedEventArgs) Try Dim s As String = e.Result Dim xml As XElement = XElement.Parse(s) Dim listeArtistes = From p In xml.Elements("Artiste") _ Select p For Each p In listeArtistes .. nom dans p.Element("Nom").Value .. .. année de naissance dans p.Element("AN").Value Next p Catch exc As Exception ..... End Try End Sub Application à un service Web météo WebClient client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); string url = "http://www.webServiceX.net/GlobalWeather.asmx/" + "GetCitiesByCountry?CountryName=France"; client.DownloadStringAsync(new Uri(url), 0); ..... List listeVilles; void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { try { string s = e.Result; s = s.Replace("<", "<"); s = s.Replace(">", ">"); XDocument xml = XDocument.Parse(s); XNamespace xmlns = "http://www.webserviceX.NET"; var q = from p in xml.Elements(xmlns + "string") .Elements(xmlns + "NewDataSet") .Elements(xmlns + "Table") select p.Element(xmlns + "City").Value ; listeVilles = q.ToList(); } catch (Exception exc) { ..... // traiter l’erreur } } Imports System.Xml.Linq Imports System.Net ..... Dim client = New WebClient() client.DownloadStringAsync( _ New Uri("http://www.webServiceX.net/GlobalWeather.asmx/" & "GetCitiesByCountry?CountryName=France"), 0) AddHandler client.DownloadStringCompleted, _ AddressOf client_DownloadStringCompleted ..... Dim listeVilles As List(Of String) Private Sub client_DownloadStringCompleted(ByVal sender As Object, _ ByVal e As DownloadStringCompletedEventArgs) Try Dim s As String = e.Result s = s.Replace("<", "<") s = s.Replace(">", ">") Dim xml As XDocument = XDocument.Parse(s) Dim xmlns As XNamespace = "http://www.webserviceX.NET" Dim q = From p In xml.Elements(xmlns + "string").Elements(xmlns + "NewDataSet").Elements(xmlns + "Table") _ Select p.Element(xmlns + "City").Value listeVilles = q.ToList() Catch exc As Exception ..... ' traiter l’erreur End Try End Sub if (xml.Elements(xmlns + "string").First().Value == "") s = "Pas de météo pour ce pays"; Calvi, France (LFKC) 42-32N 008-48E 58M from the W (260 degrees) at 7 MPH (6 KT) (direction variable):0 greater than 7 mile(s):0 66 F (19 C) 30 F (-1 C) 25% 30.00 in. Hg (1016 hPa) Success WebClient client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); string url = "http://www.webServiceX.net/GlobalWeather.asmx/" + "GetWeather?CityName=Calvi&CountryName=France"; client.DownloadStringAsync(new Uri(url), 0); string s = e.Result; s = s.Replace("<", "<"); s = s.Replace(">", ">"); s = s.Replace("", ""); XElement xml = XElement.Parse(s); XNamespace ns = "http://www.webserviceX.NET"; string temp = xml.Elements(ns + "CurrentWeather") .Elements(ns+"Temperature").First().Value; // éliminer la valeur Fahrenheit (degrés Celsius entre parenthèses) int n1 = temp.IndexOf("("); int n2 = temp.IndexOf(")"); temp = temp.Substring(n1 + 1, n2 - n1-2); Application au service Web Flickr string clé = .. votre identifiant Flickr ici .. string url = "http://api.flickr.com/services/rest/" + "?method=flickr.photos.search&api_key=" + clé + "&text=" + sCrit; WebClient client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); client.DownloadStringAsync(new Uri(url), 0); var liste = from p in xml.Element("rsp").Element("photos").Elements("photo") select p ; liste = liste.Take(12); .. nombre de photos donné par liste.Count(), soit ici 12 ou moins .. foreach (var ph in liste) // on passe en revue les photos { .. titre d’une photo dans ph.Attribute("title").Value .. } string sFarm = liste.ElementAt(N).Attribute("farm").Value; string sServer = liste.ElementAt(N).Attribute("server").Value; string sId = liste.ElementAt(N).Attribute("id").Value; string sSecret = liste.ElementAt(N).Attribute("secret").Value; string sUrl = "http://farm" + sFarm + ".static.flickr.com/" + sServer + "/" + sId + "_" + sSecret + ".jpg"; ImageSource imgs = new System.Windows.Media.Imaging.BitmapImage(new Uri(sUrl)); img.SetValue(Image.SourceProperty, imgs);